package com.yzion.baselib.app

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Process
import android.text.TextUtils
import android.view.View
import androidx.multidex.MultiDexApplication
import com.yzion.baselib.dialog.BaseDialog
import com.yzion.baselib.dialog.BaseLoadingDialog
import com.yzion.baselib.widget.toast.DefaultToast
import com.yzion.baselib.toast.IToast
import com.yzion.baselib.widget.dialog.DefaultLoadingDialog
import com.yzion.baselib.widget.titlebar.DefaultTitleBar
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator
import me.yokeyword.fragmentation.anim.FragmentAnimator

/**
 * 继承 MultiDexApplication 已支持 MultiDex 初始化
 */
open class BaseApp : MultiDexApplication(), IAppProvider {

    override fun onCreate() {
        super.onCreate()
        app = this
    }

    fun isAppMainProcess(): Boolean {
        return try {
            val pid = Process.myPid()
            val process = getAppNameByPID(app, pid)
            if (TextUtils.isEmpty(process)) {
                true
            } else packageName == process
        } catch (e: Exception) {
            e.printStackTrace()
            true
        }
    }

    private fun getAppNameByPID(context: Context, pid: Int): String {
        val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (processInfo in manager.runningAppProcesses) {
            if (processInfo.pid == pid) {
                return processInfo.processName
            }
        }
        return ""
    }

    /**
     * 创建默认样式的加载弹窗
     */
    override fun createLoadingDialog(context: Context): BaseLoadingDialog {
        return DefaultLoadingDialog(context)
    }

    /**
     * 创建默认样式的Toast
     */
    override fun createToastView(context: Context): IToast {
        return DefaultToast(context)
    }

    /**
     * 创建默认样式的标题栏
     */
    override fun <TitleBar : View> createTitleBarView(context: Context): TitleBar {
        return DefaultTitleBar(context) as TitleBar
    }

    /**
     * 创建默认的界面切换动画
     */
    override fun createFragmentAnimator(): FragmentAnimator {
        return DefaultHorizontalAnimator()
    }

    companion object {
        lateinit var app: Application
            private set
    }
}

/*  依赖的库

    //fragment框架
    api project(':fragmentlib')

    //AndroidX相关
    api 'androidx.appcompat:appcompat:1.2.0'
    api 'androidx.legacy:legacy-support-v4:1.0.0'
    api 'androidx.annotation:annotation:1.2.0'
    api 'androidx.recyclerview:recyclerview:1.2.0'
    api 'androidx.constraintlayout:constraintlayout:2.0.4'
    api 'androidx.viewpager2:viewpager2:1.0.0'
    api 'androidx.multidex:multidex:2.0.1'
    api 'com.google.android.material:material:1.2.1'

    //单元测试相关
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    //图片加载框架
    api "com.github.bumptech.glide:glide:4.12.0"
    kapt "com.github.bumptech.glide:compiler:4.12.0"
    api 'com.facebook.fresco:fresco:2.2.0'
    //图片处理各种转换
    api "jp.wasabeef:glide-transformations:4.0.1"
    //图片滤镜
    api "jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1"
    //webp支持
    api "com.zlc.glide:webpdecoder:2.0.4.11.0"

    //Luban
    api 'top.zibin:Luban:1.1.8'

    //网络框架相关
    //https://square.github.io/retrofit/
    api "com.google.code.gson:gson:2.8.6"
    api 'com.squareup.retrofit2:retrofit:2.9.0'
    api 'com.squareup.retrofit2:converter-gson:2.9.0'
    api 'com.squareup.okhttp3:okhttp:4.9.0'
    api 'com.squareup.okhttp3:logging-interceptor:4.9.0'

    //安卓工具库
    //https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/README-CN.md
    api "com.blankj:utilcodex:1.30.6"

    //Mina，Socket框架
    api 'org.rxtx:rxtx:2.1.7'

    //greendao数据库框架
    //https://github.com/greenrobot/greenDAO
    api "org.greenrobot:greendao:3.3.0"
    //greendao数据库升级帮助类
    //https://github.com/yuweiguocn/GreenDaoUpgradeHelper/blob/master/README_CH.md
    api 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1'

    //数据库框架-Room
    api "androidx.room:room-runtime:2.2.5"
    kapt "androidx.room:room-compiler:2.2.5"
    api "androidx.room:room-ktx:2.2.5"

    //二维码库,ZXing的精简版
    //https://github.com/jenly1314/ZXingLite
    api 'com.king.zxing:zxing-lite:2.0.3'

    //RxJava
    //https://github.com/ReactiveX/RxJava
    api "io.reactivex.rxjava2:rxjava:2.2.18"
    //RxAdnroid
    //https://github.com/ReactiveX/RxAndroid
    api "io.reactivex.rxjava2:rxandroid:2.1.1"

    //动画库
    //https://github.com/daimajia/AndroidViewAnimations
    api 'com.daimajia.easing:library:2.4@aar'
    api 'com.daimajia.androidanimations:library:2.4@aar'

    //kotlin
    api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8"
    api 'androidx.core:core-ktx:1.6.0-beta02'

    // Anko Commons
    api "org.jetbrains.anko:anko-commons:0.10.8"
    // Anko Layouts
    api "org.jetbrains.anko:anko-sdk27:0.10.8"
    // Coroutine listeners for Anko Layouts
    api "org.jetbrains.anko:anko-sdk27-coroutines:0.10.8"

    //commons-lang3工具包
    api 'org.apache.commons:commons-lang3:3.9'

    //MMKV——基于 mmap 的高性能通用 key-value 组件
    //https://github.com/Tencent/MMKV
    api 'com.tencent:mmkv-static:1.2.4'

    //svga动画库
    //https://github.com/svga/SVGAPlayer-Android
    api 'com.github.svga:SVGAPlayer-Android:2.5.15'

    api 'me.jessyan:autosize:1.2.1'

    //Exoplayer
    api 'com.google.android.exoplayer:exoplayer:2.12.2'

    //fastjson
    api 'com.alibaba:fastjson:1.2.73'
 */